package top.hjcwzx.wrench.limiter.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.hjcwzx.wrench.limiter.aop.IpRateLimiterAspect;
import top.hjcwzx.wrench.limiter.aop.KeyRateLimiterAspect;
import top.hjcwzx.wrench.limiter.strategy.algorithm.RateLimiterAlgorithmStrategyContext;
import top.hjcwzx.wrench.limiter.strategy.algorithm.RateLimiterAlgorithmExecutor;
import top.hjcwzx.wrench.limiter.strategy.key.RateLimitKeyExecutor;
import top.hjcwzx.wrench.limiter.strategy.key.RateLimiterKeyStrategyContext;

@Configuration
@ConditionalOnClass(RateLimiterAlgorithmExecutor.class)
@EnableConfigurationProperties(value = {
        RateLimiterMemoryAutoProperties.class,
        RateLimiterTokenBucketAutoProperties.class,
        RateLimiterLeakyBucketAutoProperties.class,
        RateLimiterIpListAutoProperties.class
})
public class RateLimiterAutoConfiguration {

    /**
     * Ip限流切面
     */
    @Bean("wrenchKitIpRateLimiterAspect")
    public IpRateLimiterAspect ipRateLimiterAspect() {
        return new IpRateLimiterAspect();
    }

    /**
     * key限流切面
     */
    @Bean("wrenchKitKeyRateLimiterAspect")
    public KeyRateLimiterAspect keyRateLimiterAspect() {
        return new KeyRateLimiterAspect();
    }

    /**
     * key策略上下文
     */
    @Bean("rateLimiterKeyStrategyContext")
    public RateLimiterKeyStrategyContext rateLimiterKeyStrategyContext() {
        return new RateLimiterKeyStrategyContext();
    }

    /**
     * key策略执行器
     */
    @Bean("rateLimitKeyExecutor")
    public RateLimitKeyExecutor rateLimitKeyExecutor() {
        return new RateLimitKeyExecutor();
    }


    /**
     * 限流策略上下文
     */
    @Bean("rateLimiterAlgorithmStrategyContext")
    public RateLimiterAlgorithmStrategyContext rateLimiterAlgorithmStrategyContext() {
        return new RateLimiterAlgorithmStrategyContext();
    }

    /**
     * 限流执行器
     */
    @Bean("wrenchKitRateLimiterExecutor")
    public RateLimiterAlgorithmExecutor wrenchKitRateLimiterExecutor() {
        return new RateLimiterAlgorithmExecutor();
    }



}
